//
//  209. 长度最小的子数组.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/8/22.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation
class Solution209 {
    // 实际上是一个滑动窗口
    // 用一个起始指针标识起始位置
    func minSubArrayLen(_ s: Int, _ nums: [Int]) -> Int {
        var minLen: Int = .max
        var sum = 0
        var left = 0
        
        for right in 0 ..< nums.count {
            sum += nums[right]
            while sum >= s {
                minLen = min(right - left + 1, minLen)
                sum -= nums[left]
                left += 1
            }
        }
        return minLen == .max ? 0 : minLen
    }
    static func test() {
        let solution = Solution209()
        let nums = [1,2,3,4,5]
        let target = 11
        let result = solution.minSubArrayLen(target, nums)
        print(result)
    }
}
